﻿@model IEnumerable<Attachment>
@{
    string tenantTypeId = Request.QueryString.Get<string>("tenantTypeId", string.Empty);
}
@if (Model != null && Model.Count() != 0)
{ 
    
        <div class="tn-text-heading tn-border-bottom tn-border-gray">
        <div class="tn-option">
            <input type="checkbox" checked="checked" id="sellCheckButton" class="tn-checkbox" />
            <label for="sellCheckButton">
                出售</label></div>
        <strong>文件列表</strong>
    </div> 
    <div class="tnc-file-gallery">
        <ul class="tnc-file-list">
            @foreach (var attachment in Model)
            {               
                <li id="attachmentId_@(attachment.AttachmentId)" value="@attachment.AttachmentId" class="tnc-file-item ">
                    <span class="tn-bg-deep tn-action">
                        @Html.AjaxDeleteButton(new AjaxDeleteButton()
                        .SetConfirm("确定要删除文件？")
                        .SetText("删除")
                        .SetUrl(SiteUrls.Instance()
                     ._DeleteAttachment(tenantTypeId, attachment.AttachmentId))
                        .MergeHtmlAttribute("class", "tn-icon tn-smallicon-cross")
                        .MergeHtmlAttribute("plugin", "tipsy")
                        .MergeHtmlAttribute("title", "删除文件")
                         .MergeHtmlAttribute("name", "operate_" + (attachment.AttachmentId))
                        .MergeHtmlAttribute("style", "display:none")
                         .SetDeleteTarget("#attachmentId_" + attachment.AttachmentId))
                    </span>
                    <p name="attachmentSelect" class="tnc-file-wrap tn-widget-content tn-border-gray">
                        <a title="点击文件插入编译器" href="javascript:;">@Html.ContentTypeThumbnail(attachment.FriendlyFileName, ContentTypeThumbnailSize.Large)</a>
                    </p>
                    <div class="tnc-sale-price" value="@attachment.Price">
                        @if (attachment.Price > 0)
                        { 
                            <em title="点击修改售价" class="tn-nums">售:@attachment.Price</em>
                        }
                    </div>
                    <p class="tn-text-heading" title="@(attachment.FriendlyFileName)">
                        @(StringUtility.Trim(attachment.FriendlyFileName, 5))
                    </p>
                    @{
                    string renderHtml;
                    }
                    @if (attachment.ContentType.IndexOf("application/x-shockwave-flash") > -1)
                    {
                        renderHtml = string.Format("<object width=\"400\" height=\"400\" data=\"{0}\" type=\"application/x-shockwave-flash\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"src\" value=\"{0}\" /></object>", SiteUrls.Instance().AttachmentDirectUrl(attachment));
                    }
                    else if (attachment.ContentType.IndexOf("video/quicktime") > -1)
                    {
                        renderHtml = string.Format("<object classid=\"clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b\" width=\"100\" height=\"100\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0\">"
                        + "<param name=\"src\" value=\"{0}\" /><embed type=\"video/quicktime\" width=\"100\" height=\"100\" src=\"{0}\"></embed>"
                        + "</object>", SiteUrls.Instance().AttachmentDirectUrl(attachment));
                    }
                    else if (attachment.ContentType.IndexOf("video/x-ms-wmv") > -1)
                    {
                        renderHtml = string.Format("<object classid=\"clsid:6bf52a52-394a-11d3-b153-00c04f79faa6\" width=\"100\" height=\"100\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\">"
                            + "<param name=\"url\" value=\"{0}\" /><embed type=\"application/x-mplayer2\" width=\"100\" height=\"100\" src=\"{0}\"></embed></object>", SiteUrls.Instance().AttachmentDirectUrl(attachment));
                    }
                    else if (attachment.ContentType.IndexOf("audio/x-pn-realaudio-plugin") > -1)
                    {
                        renderHtml = string.Format("<object classid=\"clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa\" width=\"100\" height=\"100\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\">"
                            + "<param name=\"src\" value=\"{0}\" /><embed type=\"audio/x-pn-realaudio-plugin\" width=\"100\" height=\"100\" src=\"{0}\"></embed>"
                            + "</object>", SiteUrls.Instance().AttachmentDirectUrl(attachment));
                    }
                    else
                    {
                        renderHtml = string.Format("[attach:{0}]", attachment.AttachmentId);
                    }
                    <input type="hidden" name="renderHtml" value="@renderHtml" />
                </li>  
            }
        </ul>
    </div> 
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#sellCheckButton").click(function () {
            if (this.checked) {
                $("div.tnc-sale-price[value='0']").show();
            }
            else
                $("div.tnc-sale-price[value='0']").hide();
        });
        //设置售价
        $("div.tnc-sale-price").each(function () {

            var attId = $(this).parents("li:first").attr("value");
            var price = $(this).attr("value");
            $(this).editable("@Html.Raw(SiteUrls.Instance()._SavePrice(tenantTypeId))", {
                indicator: '<div class="tn-loading"></div>',
                name: "price",
                method: "POST",
                width: "55px",
                select: true,
                submitdata: { attachementId: attId },
                placeholder: '<a href="javascript:;" class="tn-edit">添加售价</a>',
                tooltip: "",
                style: "inherit",
                ajaxoptions: { dataType: 'json' },
                onedit: function (settings, self) {
                    $(self).html($(self).attr("value")).attr("title", "按回车键进行提交");
                },
                callback: function (data, settings) {
                    function formatPrice(value) {
                        if (value && value > 0) {
                            return '<em title="点击修改售价" class="tn-nums">售:' + value + '</em>';
                        }
                        else
                            return '<a href="javascript:;" class="tn-edit">添加售价</a>';
                    }
                    if (data.MessageContent) {
                        $(this).html(formatPrice(price));
                        art.dialog.tips(data.MessageContent, 2, data.MessageType);
                    }
                    else
                        $(this).html(formatPrice(data.price)).attr("value", data.price);
                }
            });
        });

        $("p[name='attachmentSelect'] a").click(function () {
            var $li = $(this).parents("li:first");
            var $renderHtml = $li.find('input[name="renderHtml"]');
            var price = $li.find("div.tnc-sale-price").attr("value");
            var $tinyMCE = $('#' + tinyMCE.activeEditor.id);
            if ($renderHtml) {
                if (price > 0)
                    $tinyMCE.insertContentToEditor('[attach:' + $li.attr("value") + ']');
                else
                    $tinyMCE.insertContentToEditor($renderHtml.attr("value"));
                return false;
            }
        });
        $("li[id^='attachmentId_']", $("ul.tnc-file-list")).hover(function () {
            $("a[name='operate_" + $(this).attr("id").replace(/[^0-9]/ig, "") + "']").show();
            $(this).addClass("tn-hover"); $(this).find("p").first().addClass("tn-border-deep tn-bg-light").removeClass("tn-border-gray");
        },
        function () {
            $("a[name='operate_" + $(this).attr("id").replace(/[^0-9]/ig, "") + "']").hide();
            $(this).removeClass("tn-hover"); $(this).find("p").first().removeClass("tn-border-deep tn-bg-light").addClass("tn-border-gray");
        });
    });
    function selectAttachments() {
        $('p[name="attachmentSelect"] a').click();
    }

</script>
